import { Options, Vue } from 'vue-class-component'
import navigator from '@/components/common/navigator.vue'
import router from '@/router'
import store from '@/store'
import { ElMessage } from 'element-plus'

interface iChallenge {
  challengeId: number
  challengeName: string
  introduction: string
  startTime: Date
  endTime: Date
  stockCode: string
  startingFunds: number
  maxParticipantsNumber: number
  participantsNumber: number
  challengeStat: number
  createTime: Date
}

interface iChallengeEnded {
  challengeId: number
  challengeName: string
  introduction: string
  startTime: Date
  endTime: Date
  stockCode: string
  startingFunds: number
  maxParticipantsNumber: number
  participantsNumber: number
  challengeStat: number
  createTime: Date
}

@Options({
  components: {
    navigator
  }
})

export default class challenge extends Vue {
  isAdmin = '0'
  userId = '0'
  searchChallenge: any[] = []
  challenges: iChallenge[] = []
  challengesCopy: iChallenge[] = []
  challengesEnded: iChallengeEnded[] = []
  challengesUser: iChallenge[] = []
  challengesEndedUser: iChallenge[] = []
  pageSum = 1
  curPage = 1
  eachPage = 5
  pageSumUser = 1
  curPageUser = 1
  eachPageUser = 2
  activeName = 'first'
  activeNameUser = 'firstUser'
  input1 = ''
  hidden = false
  mounted () {
    this.isAdmin = store.state.admin
    this.$router.afterEach((to, from, next) => {
      window.scrollTo(0, 0)
    })
    if (store.state.u !== null) {
      this.userId = store.state.u.userId
      store.dispatch('getChallengeInProgressUser', this.userId)
        .then(response => {
          this.challengesUser = response.data.data
          this.pageSumUser = Math.ceil(this.challengesUser.length / this.eachPageUser)
          console.log(this.challengesUser.length)
        }).catch(() => {
          console.log('connect failed')
        })
    }
    store.dispatch('getChallengeInProgress')
      .then(response => {
        this.challenges = response.data.data
        this.challengesCopy = response.data.data
        this.pageSum = Math.ceil(this.challenges.length / this.eachPage)
        console.log(this.challengesCopy.length)
        console.log(this.challenges.length)
      }).catch(() => {
        console.log('connect failed')
      })
    store.dispatch('getChallengeEnded')
      .then(response => {
        this.challengesEnded = response.data.data
        console.log(this.challengesEnded[0])
      }).catch(() => {
        console.log('connect failed')
      })
    store.dispatch('getChallengeEndedUser', this.userId)
      .then(response => {
        this.challengesEndedUser = response.data.data
      }).catch(() => {
        console.log('connect failed')
      })
  }

  launch (): void {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    if (store.state.admin === 0 || store.state.admin as string === '0') {
      ElMessage.warning('没有权限')
    } else {
      router.push({
        path: '/createChallenge'
      })
    }
  }

  handleSizeChange (val) {
    console.log(`每页 ${val} 条`)
    this.curPage = 1
    this.eachPage = val
  }

  // 当前页改变时触发 跳转其他页
  handleCurrentChange (val) {
    console.log(`当前页: ${val}`)
    this.curPage = val
  }

  handleSizeChangeUser (val) {
    console.log(`每页 ${val} 条`)
    this.curPageUser = 1
    this.eachPageUser = val
  }

  // 当前页改变时触发 跳转其他页
  handleCurrentChangeUser (val) {
    console.log(`当前页: ${val}`)
    this.curPageUser = val
  }

  doSearch () {
    console.log(this.input1)
    if (this.input1 === '') {
      this.searchChallenge = []
      this.challenges = this.challengesCopy
    } else {
      if (this.searchChallenge !== []) {
        this.searchChallenge = []
      }
      for (const challengeItem of this.challengesCopy) {
        if (challengeItem.challengeName.includes(this.input1)) {
          this.searchChallenge.push(challengeItem)
          this.challenges = this.searchChallenge
        }
      }
    }
  }

  signForChallenge (challenge) {
    console.log(challenge.challengeName)
    router.push({
      path: '/challengeDetail',
      query: { challengeId: challenge.challengeId }
    })
  }
}
